package com.example.administrator.yuejian_15.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.yuejian_15.R;
import com.example.administrator.yuejian_15.fragment.JlbFragment;
import com.example.administrator.yuejian_15.fragment.LmFragment;
import com.example.administrator.yuejian_15.fragment.PxFragment;
import com.example.administrator.yuejian_15.fragment.StFragment;
import com.example.administrator.yuejian_15.fragment.WdFragment;


public class MainActivity extends BaseActivity implements View.OnClickListener {

    LinearLayout tab_px;
    LinearLayout tab_lm;
    LinearLayout tab_jlb;
    LinearLayout tab_st;
    LinearLayout tab_wd;

    ImageView img_px;
    ImageView img_lm;
    ImageView img_jlb;
    ImageView img_st;
    ImageView img_wd;

    TextView tv_px,tv_lm,tv_wd,tv_jlb,tv_st;

    Fragment fragment_px;
    Fragment fragment_jlb;
    Fragment fragment_st;
    Fragment fragment_lm;
    Fragment fragment_wd;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_main);
        tab_px = (LinearLayout) findViewById(R.id.tab_px);
        tab_jlb = (LinearLayout) findViewById(R.id.tab_jlb);
        tab_lm = (LinearLayout) findViewById(R.id.tab_lm);
        tab_st = (LinearLayout) findViewById(R.id.tab_st);
        tab_wd = (LinearLayout) findViewById(R.id.tab_wd);

        img_px = (ImageView) findViewById(R.id.img_px);
        img_jlb = (ImageView) findViewById(R.id.img_jlb);
        img_lm = (ImageView) findViewById(R.id.img_lm);
        img_wd = (ImageView) findViewById(R.id.img_wd);
        img_st = (ImageView) findViewById(R.id.img_st);

        tv_jlb = (TextView) findViewById(R.id.tv_jlb);
        tv_px = (TextView) findViewById(R.id.tv_px);
        tv_lm = (TextView) findViewById(R.id.tv_lm);
        tv_wd = (TextView) findViewById(R.id.tv_wd);
        tv_st = (TextView) findViewById(R.id.tv_st);

        selectTab(0);

    }

    @Override
    void initEvent() {
        tab_px.setOnClickListener(this);
        tab_jlb.setOnClickListener(this);
        tab_lm.setOnClickListener(this);
        tab_st.setOnClickListener(this);
        tab_wd.setOnClickListener(this);


    }

    @Override
    void initDate() {

    }



    @Override
    public void onClick(View view) {

        resetImage();
        switch (view.getId()) {
            case R.id.tab_jlb:
                selectTab(0);
                break;
            case R.id.tab_px:
                selectTab(1);
                break;
            case R.id.tab_lm:
                selectTab(2);
                break;
            case R.id.tab_st:
                selectTab(3);
                break;
            case R.id.tab_wd:
                selectTab(4);
                break;
            default:
                break;
        }

    }


    private void resetImage() {
        img_px.setImageResource(R.drawable.pxh);
        img_jlb.setImageResource(R.drawable.jlbh);
        img_lm.setImageResource(R.drawable.lmh);
        img_st.setImageResource(R.drawable.sth);
        img_wd.setImageResource(R.drawable.wdh);
    }

    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (fragment_jlb != null) {
            fragmentTransaction.hide(fragment_jlb);
        }
        if (fragment_px != null) {
            fragmentTransaction.hide(fragment_px);
        }
        if (fragment_lm != null) {
            fragmentTransaction.hide(fragment_lm);
        }
        if (fragment_st != null) {
            fragmentTransaction.hide(fragment_st);
        }
        if (fragment_wd != null) {
            fragmentTransaction.hide(fragment_wd);
        }
    }

    private void selectTab(int i) {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction);
        switch (i) {
            case 0:
                if (fragment_jlb == null) {
                    fragment_jlb = new JlbFragment();
                    fragmentTransaction.add(R.id.content, fragment_jlb);
                } else {
                    fragmentTransaction.show(fragment_jlb);
                }
                //tv_px.setTextColor(0x2c2c2cdd);
                img_jlb.setImageResource(R.drawable.jlbl);

                break;
            case 1:
                if (fragment_px == null) {
                    fragment_px = new PxFragment();
                    fragmentTransaction.add(R.id.content, fragment_px);
                } else {

                    fragmentTransaction.show(fragment_px);
                }
                //tv_jlb.setTextColor(0x2c2c2cdd);
                img_px.setImageResource(R.drawable.pxl);
                break;
            case 2:
                if (fragment_lm == null) {
                    fragment_lm = new LmFragment();
                    fragmentTransaction.add(R.id.content, fragment_lm);
                } else {
                    fragmentTransaction.show(fragment_lm);
                }
                img_lm.setImageResource(R.drawable.lml);
                //tv_lm.setTextColor(0x2c2c2cdd);
                break;

            case 3:
                if (fragment_st == null) {
                    fragment_st = new StFragment();
                    fragmentTransaction.add(R.id.content, fragment_st);
                } else {
                    fragmentTransaction.show(fragment_st);
                }
                //tv_st.setTextColor(0x2c2c2cdd);
                img_st.setImageResource(R.drawable.stl);
                break;

            case 4:
                if (fragment_wd == null) {
                    fragment_wd = new WdFragment();
                    fragmentTransaction.add(R.id.content, fragment_wd);
                } else {
                    fragmentTransaction.show(fragment_wd);
                }
                img_wd.setImageResource(R.drawable.wdl);
                //tv_wd.setTextColor(0x2c2c2cdd);
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
    }
}
